
package com.ims.common.action;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.opensymphony.xwork2.ActionSupport;



public class BaseAction extends ActionSupport {



    private static final long serialVersionUID = 262325239316668473L;
    private Integer totalSize = null;
    private Integer fetchSize = null;
    private Integer lastValue = null;
    private Integer currentlyFetched = null;
    private String errorID = new String();
    private String title = null;// store title for the page which will used in
				// header.jsp

    private String masterMenuString;
    private String saveMessage;
    private String forwardAction;
	private static Properties prop = new Properties();
    
	
	/**
     * This method returns the values
     * 
     * @return the masterMenuString
     */
    public String getMasterMenuString() {
    	return  masterMenuString;
    }

    /**
     * This method sets the value
     * 
     * @param masterMenuString
     *            the masterMenuString to set
     */
    public void setMasterMenuString(String masterMenuString) {
	this.masterMenuString = masterMenuString;
    }

    /**
     * This method returns the values
     * 
     * @return the errorID
     */
    public String getErrorID() {
	return errorID;
    }

    /**
     * This method sets the value
     * 
     * @param errorID
     *            the errorID to set
     */
    public void setErrorID(String errorID) {
	this.errorID = errorID;
    }

    /**
     * This method returns the values
     * 
     * @return the title
     */
    public String getTitle() {
	return title;
    }

    /**
     * This method sets the value
     * 
     * @param title
     *            the title to set
     */
    public void setTitle(String title) {
	this.title = title;
    }

    /**
     * This method returns the values
     * 
     * @return the totalSize
     */
    public Integer getTotalSize() {
	return totalSize;
    }

    /**
     * This method sets the value
     * 
     * @param totalSize
     *            the totalSize to set
     */
    public void setTotalSize(Integer totalSize) {
	this.totalSize = totalSize;
    }

    /**
     * This method returns the values
     * 
     * @return the fetchSize
     */
    public Integer getFetchSize() {
	return fetchSize;
    }

    /**
     * This method sets the value
     * 
     * @param fetchSize
     *            the fetchSize to set
     */
    public void setFetchSize(Integer fetchSize) {
	this.fetchSize = fetchSize;
    }

    /**
     * This method returns the values
     * 
     * @return the lastValue
     */
    public Integer getLastValue() {
	return lastValue;
    }

    /**
     * This method sets the value
     * 
     * @param lastValue
     *            the lastValue to set
     */
    public void setLastValue(Integer lastValue) {
	this.lastValue = lastValue;
    }

    /**
     * This method returns the values
     * 
     * @return the currentlyFetched
     */
    public Integer getCurrentlyFetched() {
	return currentlyFetched;
    }

    /**
     * This method sets the value
     * 
     * @param currentlyFetched
     *            the currentlyFetched to set
     */
    public void setCurrentlyFetched(Integer currentlyFetched) {
	this.currentlyFetched = currentlyFetched;
    }
    


    /**
     * method to set values for pagenation fields at the begining of the page
     */
    public void setPagenationFields() {
	setLastValue(10);
	setCurrentlyFetched(1);
	setFetchSize(100);
    }
/*
    *//**
     * This method is used to save the Error and to throw the appropriate
     * message for each CustomException
     * 
     * @param customException
     *//*
    public void saveError(Exception customException) {
	Object[] values = null;
	MessageFormat messageFormat = null;
	values = (Exception).getValues();
	messageFormat = new MessageFormat(getText(customException
			.getMessageKey()));
	if(values != null && values.length > 0) {
	    addActionError(getText(messageFormat
			    .format(getLocalizationValues(values))));
	} else {
	    addActionError(getText(customException.getMessageKey()));
	}
    }*/

    /**
     * Method to diplay the error message with Place Holder values.
     * 
     * @param messageKey
     *            contains message key of the property
     * @param values
     *            contains message value for the Place Holder
     */
    public void saveActionError(String messageKey, String... values) {
	MessageFormat messageFormat = null;
	messageFormat = new MessageFormat(getText(messageKey));
	if(values != null && values.length > 0) {
	    addActionError(getText(messageFormat
			    .format(getLocalizationValues(values))));
	} else {
	    addActionError(getText(messageKey));
	}
    }

    /**
     * Method to localize the place holder value passed to the Error Messages.
     * 
     * @param values
     *            contains the placeholder values.
     * @return array of objects with Localized values.
     */
    private Object[] getLocalizationValues(Object[] values) {
	Object[] messageValues = null;
	int index = 0;
	if(values != null && values.length > 0) {
	    messageValues = new Object[20];
	    for(Object messageKey: values) {
		messageValues[index] = getText((String) messageKey);
		index++;
	    }
	}
	return messageValues;
    }

    /**
     * Method to diplay the error message.
     * 
     * @param messageKey
     *            contains message key of the property
     */
    public void saveActionError(String messageKey) {
	addActionError(getText(messageKey));
    }

/*    *//**
     * get Status list values
     * 
     * @return
     *//*
    public List<codeValue> getStatusList() {
	if(this.statusList == null || this.statusList.isEmpty()) {
	    PickListManager pickListManager = PickListManager.getInstance();
	    this.statusList = pickListManager
			    .getListByKey(PickListManager.STATUS_KEY);
	}
	return statusList;
    }

    public void setStatusList(List<codeValue> statusList) {
	this.statusList = statusList;
    }

    *//**
     * Get Status list values for search panels based on Picklistmanager and
     * cache coordination.
     * 
     * @return
     *//*
    public List<codeValue> getSearchStatusList() {
	if(this.searchStatusList == null || this.searchStatusList.isEmpty()) {
	    PickListManager pickListManager = PickListManager.getInstance();
	    this.searchStatusList = pickListManager
			    .getListByKey(PickListManager.SEARCH_STATUS_KEY);
	}
	return searchStatusList;
    }*/

    
    


    /**
     * Overridden method to set the error id along with error message so as to
     * set the focus to the error field.
     * 
     * @param errorMsg
     *            .
     * @param errorID
     *            .
     */

    public void addActionError(String messageKey, String errorID, String... values) {
	MessageFormat messageFormat = null;
	messageFormat = new MessageFormat(getText(messageKey));
	if(values != null && values.length > 0) {
	    addActionError(getText(messageFormat
			    .format(getLocalizationValues(values))));
	} else {
	    addActionError(getText(messageKey));
	}
	setErrorID(errorID);
    }
    
    /**
     * Add message to show the confirmation after save and set the redirect action.
     * @param messageKey	holds the confirmation message for save.
     * @param forwardAction	holds the redirect action. 
     * @param values		holds the place holder values.
     */
    public void addSaveMsgWithForwardAction(String messageKey,String forwardAction,String... values){
	MessageFormat messageFormat = null;
	messageFormat = new MessageFormat(getText(messageKey));
	if(values != null && values.length > 0) {
	    setSaveMessage(getText(messageFormat
			    .format(getLocalizationValues(values))));
	}else{
	    setSaveMessage(getText(messageKey));
	}
	setForwardAction(forwardAction);
    }

    /**
     * Add message to show the confirmation after save and set the redirect action.
     * @param messageKey	holds the confirmation message for save.
     * @param forwardAction	holds the redirect action. 
     */
    public void addSaveMsgWithForwardAction(String messageKey,String forwardAction){
	setSaveMessage(getText(messageKey));
	setForwardAction(forwardAction);
    }

    /**
     * Add message to show the confirmation after save.
     * @param messageKey  holds the save message property key. 
     */
    public void addSaveMsg(String messageKey){
	setSaveMessage(getText(messageKey));
    }

    /**
     * Add message to show the confirmation after save. 
     * @param messageKey	holds the save message property key.
     * @param values		holds the place holder values.
     */
    public void addSaveMsg(String messageKey,String... values){
	MessageFormat messageFormat = null;
	messageFormat = new MessageFormat(getText(messageKey));
	if(values != null && values.length > 0) {
	    setSaveMessage(getText(messageFormat
			    .format(getLocalizationValues(values))));
	}else{
	    setSaveMessage(getText(messageKey));  
	}
    }

    /**
     * This method returns the values
     * @return the saveMessage
     */
    public String getSaveMessage() {
        return saveMessage;
    }

    
    /**
     * This method sets the value
     * @param saveMessage the saveMessage to set
     */
    public void setSaveMessage(String saveMessage) {
        this.saveMessage = saveMessage;
    }

    
    /**
     * This method returns the values
     * @return the forwardAction
     */
    public String getForwardAction() {
        return forwardAction;
    }

    
    /**
     * This method sets the value
     * @param forwardAction the forwardAction to set
     */
    public void setForwardAction(String forwardAction) {
        this.forwardAction = forwardAction;
    }
    
    
    /**
	 * Read the value from config.properties
	 * 
	 * @param key
	 * @return
	 */
	public static String getConfig(String key) {

		if (prop == null || prop.isEmpty()) {
	//		prop = PropertyLoader.loadProperties("config.properties");
		}

		return prop.getProperty(key);
	}
    
    
    
    
 }